package com.molinari.coding.activities.addconto;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.molinari.coding.Cella;
import com.molinari.coding.ControllerCoding;
import com.molinari.coding.R;
import com.molinari.coding.activities.CodingBaseActivity;
import com.molinari.coding.dati.domain.dao.WrapCella;

public class AggiungiContoActivity extends CodingBaseActivity {

	private Button btnAddConto;
	
	@Override
	protected int getIdLayout() {
		return R.layout.aggiungiconto;
	}

	@Override
	protected void onCreateCustom(Bundle savedInstanceState) throws Exception {
		final boolean nonCreato = !ControllerCoding.hasConto(getApplicationContext());
		final Cella[] celle = creaArray(nonCreato);
		final CelleAdapter adapter = new CelleAdapter(this, this, R.layout.rowconto, celle);
			
		final ListView list = (ListView) findViewById(R.id.listView1);
		list.setAdapter(adapter);
		
		btnAddConto = loadButton(btnAddConto, R.id.btnAddConto);
		btnAddConto.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				riempiCelle(list, celle);
				
				
				if(checkCelle(celle)){
					
					boolean caricamentoOk = true;
					try{
						for (int i = 0; i < celle.length; i++) {

							Cella cella = celle[i];
							com.molinari.coding.dati.domain.Cella cellaDom = new com.molinari.coding.dati.domain.Cella();
							cellaDom.setChiave(cella.getChiave());
							cellaDom.setUno(cella.getUno());
							cellaDom.setDue(cella.getDue());
							cellaDom.setTre(cella.getTre());
							cellaDom.setIdUtente(ControllerCoding.getSingleton().getUtenteLogin().getidUtente());
							if(nonCreato){
								boolean insert = new WrapCella(getApplicationContext()).insert(cellaDom);
								if(!insert){
									caricamentoOk = false;
								}
							}else{
								boolean update = new WrapCella(getApplicationContext()).update(cellaDom);
								if(!update){
									caricamentoOk = false;
								}
							}
						}
						
						ControllerCoding.signContoAsCreated();
					}catch (Exception e) {
						caricamentoOk = false;
						Log.e("Errore creando le celle", e.toString());
					}
					if(caricamentoOk){
						Toast.makeText(getApplicationContext(), "Inserimento dati completato", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(getApplicationContext(), "Qualcosa è andato storto, riprova", Toast.LENGTH_SHORT).show();
					}
				}else{
					Toast.makeText(getApplicationContext(), "Completa tutte le celle", Toast.LENGTH_SHORT).show();
				}
			}

			private void riempiCelle(ListView list, Cella[] celle) {
				for (int i = 0; i < list.getChildCount(); i++) {
					
					View view = list.getChildAt(i);
					if(view != null){
						
						TextView textNum = (TextView) view.findViewById(R.id.textNumber);
						if(textNum != null){
							
							int index = Integer.parseInt(textNum.getText().toString()) -1;
							Cella cella = celle[index];
							EditText editUno = (EditText) view.findViewById(R.id.editAggiungiPrimo);
							if(editUno != null && editUno.getText() != null){
								cella.setUno(editUno.getText().toString());
							}
							
							EditText editDue = (EditText) view.findViewById(R.id.editAggiungiSecondo);
							if(editDue != null && editDue.getText() != null){
								cella.setDue(editDue.getText().toString());
							}
							
							EditText editTre = (EditText) view.findViewById(R.id.editAggiungiTerzo);
							if(editTre != null && editTre.getText() != null){
								cella.setTre(editTre.getText().toString());
							}
						}
					}
				}
				
			}
		});
	}
	
	private boolean checkCelle(final Cella[] celle) {
		boolean ret = true;
		
		for (int i = 0; i < celle.length; i++) {
			
			Cella cella = celle[i];
			if(cella.getUno() == null || cella.getUno().trim().equals("") ||
			   cella.getDue() == null || cella.getDue().trim().equals("") ||
			   cella.getTre() == null || cella.getTre().trim().equals("")) {
				
				return false;
			}
		}
		return ret;
	}
	
	public Cella[] creaArray(boolean nonCreato){
		ArrayList<Cella> celle = new ArrayList<Cella>();
		if(nonCreato){
			for (int i = 1; i <= 24; i++) {
				Cella cella = new Cella();
				cella.setChiave(Integer.toString(i));
	//			cella.setUno("1");
	//			cella.setDue("2");
	//			cella.setTre("3");
				celle.add(cella);
			}
		}else{
			int idUtente = ControllerCoding.getSingleton().getUtenteLogin().getidUtente();
			Iterator<com.molinari.coding.dati.domain.Cella> iterCelle = (Iterator<com.molinari.coding.dati.domain.Cella>) new WrapCella(getApplicationContext()).selectByIdUtente(idUtente);
			while (iterCelle.hasNext()) {
				com.molinari.coding.dati.domain.Cella cellaDom = (com.molinari.coding.dati.domain.Cella) iterCelle.next();
				Cella cella = new Cella();
				cella.setChiave(cellaDom.getChiave());
				cella.setUno(cellaDom.getUno());
				cella.setDue(cellaDom.getDue());
				cella.setTre(cellaDom.getTre());
				celle.add(cella);
			}
			Collections.sort(celle, new Comparator<Cella>() {
				public int compare(Cella cella1, Cella cella2) {
					return new Integer(cella1.getChiave()).compareTo(new Integer(cella2.getChiave()));
				};
			});
		}
		
		
		return celle.toArray(new Cella[celle.size()]);
	}
}
